"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】创建事件触发器，关联函数存在但返回类型不是event_trigger，创建失败
Create At   :   2024/07/13
Owner       :   sungang14
Description :
    预置条件：
        1、创建函数：CREATE OR REPLACE FUNCTION f_eventtrigger_0040_1() RETURNS integer AS $$
            BEGIN
            RETURN 1 + 1;
            END;
            $$ LANGUAGE plpgsql;
        2、创建函数：CREATE OR REPLACE FUNCTION f_eventtrigger_0040_2() RETURNS TRIGGER AS
           $$
           DECLARE
           BEGIN
                   INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);
                   RETURN NEW;
           END
           $$ LANGUAGE PLPGSQL;
    测试步骤：
        1、CREATE EVENT TRIGGER evtri1_eventtrigger_0040 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0040_1();
        2、CREATE EVENT TRIGGER evtri2_eventtrigger_0040 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0040_2();
Expect      : 
        1、创建失败，报错：function xxx must return type event trigger
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists evtri1_eventtrigger_0040;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists evtri2_eventtrigger_0040;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0040_1();")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0040_2();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0040_1() RETURNS integer AS \$\$" \
            f" BEGIN" \
            f" RETURN 1 + 1;" \
            f" END;" \
            f" \$\$ LANGUAGE plpgsql;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0040_2() RETURNS TRIGGER AS \$\$" \
            f" DECLARE" \
            f" BEGIN" \
            f" INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3);" \
            f" RETURN NEW;" \
            f" END" \
            f" \$\$ LANGUAGE PLPGSQL;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)

    def test_Index_file_damaged(self):
        self.logger.info(
            "创建事件触发器:CREATE EVENT TRIGGER evtri1_eventtrigger_0040 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0040_1();")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER evtri1_eventtrigger_0040 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0040_1();")
        self.logger.info(msg)
        self.assertEqual(f'ERROR:  function "f_eventtrigger_0040_1" must return type "event_trigger"', msg)

        self.logger.info(
            "创建事件触发器:CREATE EVENT TRIGGER evtri2_eventtrigger_0040 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0040_2();")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER evtri2_eventtrigger_0040 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0040_2();")
        self.logger.info(msg)
        self.assertEqual(f'ERROR:  function "f_eventtrigger_0040_2" must return type "event_trigger"', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0040_1();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0040_2();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
